import AntModal from "@/components/pt/dialog/AntModal";
import {
  getBizMonthDept,
  addBizMonthDept,
  updateBizMonthDept,
  findMaxSort,
} from "@/api/biz/bizMonthDept";

export default {
  name: "CreateForm",
  props: {
    modelListOptions: {
      type: Array,
      required: true,
    },
  },
  components: {
    AntModal,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加月报管理",
      // 表单参数
      form: {},
      rules: {
        times: [{ required: true, message: "请选择年份", trigger: "change" }],
        modelId: [{ required: true, message: "请选择模版", trigger: "change" }],
      },
      yearShowOne: false,
      yearFormat: "YYYY",
      loadText: "保 存",
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        userId: undefined,

        deptId: undefined,

        userName: undefined,

        deptName: undefined,

        companyId: undefined,

        name: undefined,

        times: undefined,

        type: undefined,

        sort: undefined,

        status: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加月报管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const bizMonthDeptId = row.id;
      getBizMonthDept(bizMonthDeptId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改月报管理";
        this.spinning = !this.spinning;
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.loadText = "任务创建中....";
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateBizMonthDept(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addBizMonthDept(saveForm).then((response) => {
              this.loading = false;
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/monthdept/index";
      this.$router.push(index);
    },
    openChangeOne(status) {
      if (status) {
        this.yearShowOne = true;
      }
    },
    // 得到年份选择器的值
    panelChangeOne(value) {
      this.form.times = value;
      this.yearShowOne = false;
    },
  },
};
